<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;

class UploadController extends Controller
{
    public function insert(Request $request)
    {
    	$data = $request -> except('_token');
    	if($request -> file('content') -> isValid())
    	{
    		$file = $request -> file('content');
    		//允许的mime类型
            $allowMime = array('image/jpeg','image/png','image/gif','image/jpg');
            //获取图片的mime类型
            $mime = $file -> getmimeType();
            if(in_array($mime, $allowMime))
            {
            	//获取文件后缀名
            	$type = $file -> getClientOriginalExtension($file);
            	//设置文件新名字
            	$newName = md5(rand(10000,9999999)).time().'.'.$type;
            	//保存文件
            	$file -> move('./home/images/poster', $newName);
            	//pic重新赋值
            	$data['content'] = '/home/images/poster/'.$newName;
                //写入数据库
                $res = DB::table('post') -> insert($data);
                if($res)
                {
                    return redirect('/home/movie/details/'.$data['pid'].'/image') -> with(['info' => '上传成功']);
                }else
                {
                    return back() -> with(['info' => '上传失败']);
                }
            }else
            {
                return back() -> with(['info' => '上传失败']);
            }
    	}else
        {
            return back() -> with(['info' => '上传失败']);
        }



    }
}
